<?php

namespace app\modules\Contract\controllers;

use Yii;
use app\modules\Contract\models\Contract;
use app\modules\Contract\models\ContractSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
/**
 * ContractController implements the CRUD actions for Contract model.
 */
class ContractController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /**
     * Lists all Contract models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ContractSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        //获取所有客户
        $customer = [];
        $customer_model = Contract::find()->all();
        if($customer_model){
            foreach($customer_model as $cm){
                $customer[$cm->customer_id] = $cm->customer->name;
            }
        }   
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'customer' => $customer,
        ]);
    }

    /**
     * Displays a single Contract model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Contract model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Contract();

        $model->type = 1;

        if ($model->load(Yii::$app->request->post())) {

            if($model->type == 2){
                $t = 'FC';
            }else{
                $t = 'RC';
            }
            $d = date("Y");
            $count = Contract::find()->count();
            $num = sprintf("%05d", $count+1);
            $model->con_num = $count+1;
            $model->code = sprintf("%s%s%s", $t, $d, $num);

            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing Contract model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {

            if($model->type == 2){
                $t = 'FC';
            }else{
                $t = 'RC';
            }
            $d = date("Y");
            $num = sprintf("%05d", $model->con_num);
            $model->code = sprintf("%s%s%s", $t, $d, $num);
            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    //商务
    public function actionBusiness($id)
    {
        $model = $this->findModel($id);

        return $this->render('business', [
            'model' => $model,
        ]);
    }

    //财务
    public function actionFinance($id)
    {
        $model = $this->findModel($id);

        return $this->render('finance', [
            'model' => $model,
        ]);
    }

    //修改团队信息
    public function actionEdit(){
        
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $data = yii::$app->request->get(); 

        if(isset($data['Contract']['id'])){

             $model = Contract::findOne($data['Contract']['id']);

             $model->load($data);

             if($model->save()){

                return ['output'=>"", 'message'=>''];
             }else{
                return ['output'=>"", 'message'=>"修改失败" ];
             }

        }
    }

    //删除
    public function actionDel($id){

       
        $user_id = \yii::$app->user->id;

        $model = $this->findModel($id);

        if($model->user_id != $user_id){
            throw new NotFoundHttpException(Yii::t('app', '没有删除权限'));
            return false;
        }
        $model->status = 10;

        $model->save();

        return $this->redirect(Yii::$app->request->getReferrer());
        
    }

    /**
     * Finds the Contract model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Contract the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Contract::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
